// 引入数据库
const {
  mysqlFun
} = require('../mysql/mysql');
exports.KingHome = function (app) {
  // 密码判断
  app.post('/King', (req, res) => {
    let sql = `select * from kinghome`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        // 判断用户的密码是否和数据库中的密码一致
        if (data[0].password === Number(req.body.password)) {
          res.json({
            code: 'K0001',
            msg: '密码正确'
          })
        } else {
          // 密码不正确返回结果
          res.json({
            code: 'K0002',
            msg: '密码不正确'
          })
        }
      }
    }
    mysqlFun(sql, obj);
  })

  // 重置密码
  app.post('/KingHome', (req, res) => {
    let sql = `UPDATE kinghome SET password=${req.body.password}`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'P0002',
          msg: '密码重置成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 设置主页公告
  app.post('/KingNotice', (req, res) => {
    let sql = `UPDATE kinghome SET homeNotice='${req.body.Notice}'`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'N0001',
          msg: '主页公告重置成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 获取主页公告
  app.post('/getHomeNotice', (req, res) => {
    let sql = `select * from kinghome`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'N0001',
          msg: '获取公告数据成功',
          Notice: data[0].homeNotice
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 获取用户意见
  app.post('/KingColl', (req, res) => {
    let sql = `select * from proposal`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'C0001',
          msg: '获取用户意见成功',
          CollArr: data
        })
      }
    }
    mysqlFun(sql, obj);
  })
}